﻿using Kn.DLMS;
using Kn.DLMS.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication3;

namespace Kn.ALICE
{

    public class AliceComm
    {
        public static string ReverseConvertHexByteToString(byte[] byteArray)
        {
            StringBuilder sb = new StringBuilder(byteArray.Length);

            byte b;
            for (int i = 0; i < byteArray.Length; i++)
            {
                b = byteArray[byteArray.Length - i - 1];
                sb.Append((char)b);
            }
            return sb.ToString();
        }

    }

    #region 附录G CU序列号(模块ID)格式定义

    /// <summary>
    /// 表示用于识别模块设备的资产类型代码的枚举。
    /// </summary>
    public enum EnumAssetTypeCode : byte
    {
        CarrierModuleSingleMode, // 载波模块-单模
        CarrierModuleDualMode,   // 载波模块-双模
        LTE4GModule,             // 4G模块
    }

    public enum EnumManufacturerCode
    {
        // 01: 杭州万高
        WAN_GAO = 1,

        // 02: 江苏林洋
        LIN_YANG = 2,

        // 03: 浙江三星
        SAN_XING = 3,

        // 04: 浙江海兴
        HAI_XING = 4,


        UNKOWN01 = 5,
        UNKOWN02 = 6,
        UNKOWN03 = 7,
        UNKOWN04 = 8,
        UNKOWN05 = 9,
        UNKOWN06 = 10
    }

    public class ManufacturerCode
    {
        #region 属性
        public string strManufacturerCode { get; set; }

        private EnumManufacturerCode _manufacturerCode;
        public EnumManufacturerCode Value
        {
            get { return _manufacturerCode; }
            set
            {
                _manufacturerCode = value;
                switch (value)
                {
                    case EnumManufacturerCode.WAN_GAO:
                        strManufacturerCode = "杭州万高";
                        break;
                    case EnumManufacturerCode.LIN_YANG:
                        strManufacturerCode = "江苏林洋";
                        break;
                    case EnumManufacturerCode.SAN_XING:
                        strManufacturerCode = "宁波三星";
                        break;
                    case EnumManufacturerCode.HAI_XING:
                        strManufacturerCode = "浙江海兴";
                        break;
                    default:
                        strManufacturerCode = "未知厂家";
                        break;
                }
            }
        }

        #endregion

        #region 方法
        #endregion
    }

    public class AssetTypeCode
    {
        #region 属性
        public string strAssetTypeCode { get; set; }

        private EnumAssetTypeCode _assetTypeCode;
        public EnumAssetTypeCode Value
        {
            get { return _assetTypeCode; }
            set
            {
                _assetTypeCode = value;
                switch (value)
                {
                    case EnumAssetTypeCode.CarrierModuleSingleMode:
                        strAssetTypeCode = "载波模块-单模";
                        break;
                    case EnumAssetTypeCode.CarrierModuleDualMode:
                        strAssetTypeCode = "载波模块-双模";
                        break;
                    case EnumAssetTypeCode.LTE4GModule:
                        strAssetTypeCode = "4G模块";
                        break;
                    default:
                        strAssetTypeCode = "未知类型";
                        break;
                }
            }
        }


        #endregion

        #region 方法

        #endregion
    }

    public class GXCUModuleID
    {
        #region 属性
        /// <summary>
        /// 资产类型代码
        /// </summary>
        public AssetTypeCode assetTypeCode = new AssetTypeCode(); //模块类型
        public ManufacturerCode manufacturerCode = new ManufacturerCode(); //厂家编码
        public string strProductSerialNumber { get; set; }  //产品序列号

        private byte _cuserialNumberLen;
        private byte[] _cuserialNumber;
        public byte CUSerialNumberLen
        {
            get { return _cuserialNumberLen; }
            set
            {
                _cuserialNumberLen = value;
                _cuserialNumber = new byte[value]; // 初始化CUSerialNumber数组
            }
        }
        public byte[] CUSerialNumber
        {
            get { return _cuserialNumber; }
            set
            {
                _cuserialNumber = value;
            }
        }
        private string _strCuId;
        public string StrCuId
        {
            get { return _strCuId; }
            set { _strCuId = value; }
        }

        public string strCs { get; set; }  //校验码

        public string strUi;


        public string strCuId
        {
            get { return _strCuId; }
            set
            {
                _strCuId = value; //
                string[] separatedStrings = value.Split('-');  //001(地区)-02(资产类型)-01(万高)-xxxxxxxxxxxxxxx(产品序列号)-2(校验码)

                assetTypeCode.Value = (EnumAssetTypeCode)Convert.ToInt32(separatedStrings[1]);
                manufacturerCode.Value = (EnumManufacturerCode)(Convert.ToInt32(separatedStrings[2]));
                strProductSerialNumber = separatedStrings[3];
                strUi += " :CU序列号" + _strCuId + "解析如下:" + "\r\n";
                strUi += "地区:" + separatedStrings[0] + " | ";
                strUi += " 模块类型:" + separatedStrings[1] + assetTypeCode.strAssetTypeCode + " | "; ;
                strUi += " 厂家:" + separatedStrings[2] + manufacturerCode.strManufacturerCode + " | "; ;
                strUi += " 序列号:" + separatedStrings[3] + " | "; ;
                strUi += " 校验码:" + separatedStrings[4];
                strUi += "\r\n";

            }
        }


        public string ReverseString(string input)
        {
            char[] charArray = input.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }

        public string ConvertSerialNumber(byte[] serialNumber)
        {
            // 去掉第一个字节 0x0B
            byte[] trimmedSerialNumber = serialNumber.Skip(0).ToArray();

            // 转换为十六进制字符串
            string hexString = BitConverter.ToString(trimmedSerialNumber).Replace("-", "");

            hexString = ReverseString(hexString);
            // 插入分隔符 "-"
            string formattedSerialNumber = hexString.Insert(3, "-").Insert(6, "-").Insert(9, "-").Insert(24, "-");

            return formattedSerialNumber;
        }

        #endregion

        #region 静态方法
        public static string GetCUId(string projectCode, string manufacturerCode, string assetTypeCode, string productSerialNumber)
        {
            if (projectCode == "" || manufacturerCode == "" || assetTypeCode == "" || productSerialNumber == "") return "";

            string value = "313131313131313131313";

            projectCode = projectCode.Substring(0, 3);
            manufacturerCode = manufacturerCode.Substring(0, 2);
            assetTypeCode = assetTypeCode.Substring(0, 2);

            productSerialNumber = productSerialNumber.PadLeft(12, '0');

            string tmp = projectCode + manufacturerCode + assetTypeCode + productSerialNumber;

            byte sum = 0;

            for (int i = 0; i < value.Length; i++)
            {
                Byte NumA = Convert.ToByte(value.Substring(i, 1));
                Byte NumB = Convert.ToByte(tmp.Substring(i, 1));

                Byte Num = (Byte)(NumA * NumB);

                sum += Num;

            }

            int result = 10 - sum % 10;

            string check = result.ToString("0");

            return (projectCode + "-" + manufacturerCode + "-" + assetTypeCode + "-" + productSerialNumber + "-" + check);

        }
        #endregion


        public bool Parser()
        {
            try
            {
                strCuId = ConvertSerialNumber(_cuserialNumber);
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }

    #endregion

    #region IMEI  国际移动设备身份码
    public class IMEI
    {
        private byte _IMEILen;
        public byte IMEILen
        {
            get { return _IMEILen; }
            set
            {
                _IMEILen = value;
                if (value != 0)
                {
                    _IMEINumber = new byte[value];
                }
            }
        }

        private byte[] _IMEINumber;
        public byte[] IMEINumber
        {
            get { return _IMEINumber; }
            set
            {
                _IMEINumber = value;
            }
        }

        private string _strIMEINumber;
        public string strIMEINumber
        {
            get { return _strIMEINumber; }
            set
            {
                _strIMEINumber = value;
            }
        }


        public string strUIResult { get; set; }

        public bool Parser()
        {
            try
            {
                if (_IMEINumber != null)
                {
                    strUIResult = AliceComm.ReverseConvertHexByteToString(_IMEINumber);
                }

            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

    }

    #endregion

    #region Tpye 模块型号
    public class GXModuleModel
    {
        //型号长度
        private byte _TypeLen;
        public byte TypeLen
        {
            get { return _TypeLen; }
            set
            {
                _TypeLen = value;
                if (value != 0)
                {
                    _TypeValue = new byte[value];
                }
            }
        }

        //型号
        private byte[] _TypeValue;
        public byte[] TypeValue
        {
            get { return _TypeValue; }
            set
            {
                _TypeValue = value;
            }
        }

        public string strUIResult { get; set; }

        public bool Parser()
        {
            try
            {
                if (TypeLen != 0)
                {
                    strUIResult = AliceComm.ReverseConvertHexByteToString(_TypeValue);
                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }
    }

    #endregion

    #region 厂家标识
    public class GXManufacturerIdentifier
    {
        public byte[] Value = new byte[8];

        public string strUIResult { get; set; }

        public bool Parser()
        {
            try
            {
                strUIResult = AliceComm.ReverseConvertHexByteToString(Value);
                strUIResult = strUIResult.Replace("\0", "");

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }

    #endregion

    #region 模块软件版本
    public class GXModuleSoftVersion : GXModuleLenValue
    {
        
    }

    #endregion

    #region 模块解决方案 VANGO-BPLC-SM-SPLN: HPLC单模模块     VANGO-BPLC-DM-SPLN：HPLC双模模块
    public class GXModuleSolution : GXModuleLenValue
    {
        public GXModuleSolution()
        {
            Len = 32;
        }
    }

    #endregion

    #region Base
    public class GXModuleLenValue
    {
        private byte _Len;

        public byte Len
        {
            get { return _Len; }
            set
            {
                _Len = value;
                if (value != 0)
                {
                    Value = new byte[value];
                }
            }
        }

        private byte[] _Value;
        public byte[] Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
            }
        }

        private string _strValue;
        public string strValue
        {
            get { return _strValue; }
            set
            {
                _strValue = value;
            }
        }
        public string strUIResult { get; set; }
        public bool Parser()
        {
            try
            {
                strUIResult = AliceComm.ReverseConvertHexByteToString(Value);
                strValue = AliceComm.ReverseConvertHexByteToString(Value);

                strUIResult = strUIResult.Replace("\0", "");
                strValue = strValue.Replace("\0", "");
            }
            catch (Exception ex)
        {
                return false;
            }
            return true;
        }
    }

    #endregion
}
